package com.wsfk.ssm.action;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import com.wsfk.ssm.entity.HandBook;
import com.wsfk.ssm.entity.Picture;
import com.wsfk.ssm.entity.User;
import com.wsfk.ssm.messages.Message;
import com.wsfk.ssm.path.CallPath;
import com.wsfk.ssm.path.POPS;
import com.wsfk.ssm.service.HandBookSvc;
import com.wsfk.ssm.service.PictureSvc;
import com.wsfk.ssm.tools.BaseAction;

@Controller("HandBookAction")
public class HandBookAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7528456727349810597L;

	private HandBookSvc handBookSvc;
	
	private String httpImage;
	
	public String getHttpImage() {
		return httpImage;
	}
	
	public void setHttpImage(String httpImage) {
		this.httpImage = httpImage;
	}

	private POPS pops;

	public POPS getPops() {
		return pops;
	}
	@Resource
	public void setPops(POPS pops) {
		this.pops = pops;
	}

	
	@Resource
	public void setHandBookSvc(HandBookSvc handBookSvc) {
		this.handBookSvc = handBookSvc;
	}

	private PictureSvc pictureSvc;

	@Resource
	public void setPictureSvc(PictureSvc pictureSvc) {
		this.pictureSvc = pictureSvc;
	}

	private List<Picture> pictures;

	public List<Picture> getPictures() {
		return pictures;
	}

	public void setPictures(List<Picture> pictures) {
		this.pictures = pictures;
	}

	private Message message = new Message();

	private HandBook handBook;
	private User user;
	private List<HandBook> listBooks;
	private String prompt;
	
	
	public String getPrompt() {
		return prompt;
	}

	public void setPrompt(String prompt) {
		this.prompt = prompt;
	}

	private File upload;// 上传文件
	// 上传文件 的名称
	private String uploadFileName;

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public Message getMessage() {
		return message;
	}

	public void setHandBook(HandBook handBook) {
		this.handBook = handBook;
	}

	public HandBook getHandBook() {
		return handBook;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<HandBook> getListBooks() {
		return listBooks;
	}

	public void setListBooks(List<HandBook> listBooks) {
		this.listBooks = listBooks;
	}

	// 查询用户所创建的所有手册
	public String bookList() {
		user = (User) ServletActionContext.getRequest().getSession()
				.getAttribute("user");
		String userId = user.getId();
		this.page = handBookSvc.queryPageBookByUser(userId, page);
		return "booklist";
	}

	// 创建手册
	public String createBook() {
		return "createbook";
	}

	// 添加手册
	public String saveBook() {
		try {
			if (upload == null) {
				return "savebookfailure";
			} else {
				boolean ft = StringUtils.isBlank(handBook.getAuthor());
				if (ft) {
					handBook.setAuthor(user.getUserName());
				}
				String model = ServletActionContext.getServletContext()
						.getRealPath("WEB-INF/model");
				boolean fale = handBookSvc.savePicFile(user, handBook, upload,
						model);
				return "savebook";
			}
		} catch (Exception e) {
			return "savebookfailure";
		}
	}

	// 修改手册信息
	public String replaceBookAll() {
		httpImage = new CallPath().httpPre(pops).build();
		handBook = handBookSvc.queryById(handBook.getId());
		pictures = pictureSvc.queryAllPicByBook(handBook.getId());
		
		if (pictures.size() == 1) {
           /* user = pictureSvc .queryUserById(handBook.getUserId());*/
            prompt ="您没有上传过内容图片，无法为您显示内容图片，" ;
		}
		
		return "replacebookall";
	}

	// 更新手册
	public String updateBook() {
		int n = handBookSvc.updateHandBookById(handBook);
		if (n == 1) {
			return "updatebook";
		} else {
			return "updatebookfailure";
		}
	}

	// 查看二维码
	public String GraphicsCode() {
		httpImage = new CallPath().httpPre(pops).build();
		String handBookId = handBook.getId();
		handBook = handBookSvc.queryBookByIdAndPublishStatus(handBookId);
		return "graphicscode";
	}

	public String complete() {
		boolean fale = handBookSvc.updateVersionById(user,handBook);
		if (fale) {
			return "complete";
		} else {
			return "completefailure";
		}
	}

}
